﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EnDecrypter
{
    public partial class SettingForm : Form
    {
        public ConfigData config = new ConfigData();
        public SettingForm()
        {
            InitializeComponent();
        }

        private void radioBtn_AES_CheckedChanged(object sender, EventArgs e)
        {
            txt_AESKEY.Enabled = true;
            txt_AESIV.Enabled = true;

            txt_DESKEY.Enabled = false;
            txt_DESIV.Enabled = false;
        }

        private void radioBtn_DES_CheckedChanged(object sender, EventArgs e)
        {
            txt_AESKEY.Enabled = false;
            txt_AESIV.Enabled = false;

            txt_DESKEY.Enabled = true;
            txt_DESIV.Enabled = true;
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (radioBtn_AES.Checked)
            {
                config.ModeType = 0;
                if (string.IsNullOrWhiteSpace(txt_AESKEY.Text))
                {
                    MessageBox.Show("请输入KEY!");
                    txt_AESKEY.Focus();
                }
                if (string.IsNullOrWhiteSpace(txt_AESIV.Text))
                {
                    MessageBox.Show("请输入IV!");
                    txt_AESIV.Focus();
                }
            }
            else if (radioBtn_DES.Checked)
            {
                config.ModeType = 1;
                if (string.IsNullOrWhiteSpace(txt_DESKEY.Text))
                {
                    MessageBox.Show("请输入KEY!");
                    txt_DESKEY.Focus();
                }
                if (string.IsNullOrWhiteSpace(txt_DESIV.Text))
                {
                    MessageBox.Show("请输入IV!");
                    txt_AESIV.Focus();
                }
            }
    
            config.AesKey = txt_AESKEY.Text;
            config.AesIV = txt_AESIV.Text;

            config.DesKey = txt_DESKEY.Text;
            config.DesIV = txt_DESIV.Text;

            Config.XMLSerialize(config, Config.configPath);

            DialogResult = DialogResult.OK;
        }

        private void SettingForm_Load(object sender, EventArgs e)
        {
            if (config.ModeType == 1)
            {
                radioBtn_DES_CheckedChanged(sender, e);

            }
            txt_AESKEY.Text = config.AesKey;
             txt_AESIV.Text = config.AesIV;

            txt_DESKEY.Text = config.DesKey;
            txt_DESIV.Text = config.DesIV;
        }
    }
}
